# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy.http import Request
from SevenTimesNight.items import SeventimesnightItem

class QicilangSpider(CrawlSpider):
    name = 'qicilang'
    allowed_domains = ['7cl.in']
    # start_urls = ['http://7cl.in/']

    rules = (
        Rule(LinkExtractor(allow=r'videos/35'), callback='parse_item', follow=True),# r - 非转译  allow 连接提取的关键字（videos） callback 回调函数  follow 是否跟随所有的界面进行爬取
    )
    # 第一次爬取是模拟浏览器
    def start_requests(self):
        ua = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15"}
        yield Request("http://www.7cl.in/", headers=ua)

    def parse_item(self, response):
        item = SeventimesnightItem()
        #i['domain_id'] = response.xpath('//input[@id="
        item["content"] = response.xpath("//p[@class='title-p']/text()").extract()
        item["link"] = response.xpath("//a[@class='play-btn']/img/@src").extract()
        return item